package com.example.qts.singleTon;

import android.widget.Adapter;

import io.reactivex.Single;

public class SingleTon {

//1.饿汉式:不管需不需要,直接将对象提前创建好
//好处:线程安全
//缺点:浪费内存
//java如果没有构造,系统会提供一个默认的无参构造
//特征:
//1.app运行的过程中当前类的对象只有一个,
    private static SingleTon singleTon=new SingleTon();
    //private SingleTon(){}
   /* public static SingleTon getInstance() {
        return singleTon;
    }*/
    //1.懒汉:需要的时候才会创建
//优点:延迟了对象的实例化,
//缺点:线程不安全
   /* public static  SingleTon getInstance(){
        if (singleTon==null){
            singleTon=new SingleTon();
        }
        return singleTon;
    }*/

//DCL:Double check lock
//综合了懒汉和饿汉的有点,解决了缺点
//即延迟了对象的实例化,又保证线程安全
/*    private int a;
    private SingleTon() {
        a = 10;
    }
    public static  synchronized SingleTon getInstance(){
        if (singleTon==null){
            singleTon=new SingleTon();
        }
        return singleTon;
    }
    public static SingleTon getInstance(){
        if (singleTon==null){
            synchronized (SingleTon.class){
                if (singleTon==null){
                    singleTon=new SingleTon();
                }
            }
        }
        return singleTon;
    }*/
    private SingleTon(){}

    public static SingleTon getInstance(){
        return SingleHolder.sSingleTon4;
    }

    private static class SingleHolder {
        private static SingleTon sSingleTon4 = new SingleTon();
    }
}
